package com.tdkj.action.backstage.selfadvert;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.friendlylink.BeanFriendlylink;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.selfadvert.BeanSelfAdvert;
import com.tdkj.model.user.BeanAdminLevel;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.website.BeanProtocolDetail;
import com.tdkj.service.friendlylink.IBeanFriendlylinkService;
import com.tdkj.service.selfadvert.IBeanSelfAdvertService;


public class SelfAdvertAction extends BaseAction {
	private Long id;
	private BeanSelfAdvert advert;
	private List<BeanSelfAdvert> adverts=new ArrayList<BeanSelfAdvert>();
	private IBeanSelfAdvertService beanSelfAdvertService;
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanSelfAdvert.class);
			}
		}
		return findTextList();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public BeanSelfAdvert getAdvert() {
		return advert;
	}
	public void setAdvert(BeanSelfAdvert advert) {
		this.advert = advert;
	}
	public List<BeanSelfAdvert> getAdverts() {
		return adverts;
	}
	public void setAdverts(List<BeanSelfAdvert> adverts) {
		this.adverts = adverts;
	}
	
	public IBeanSelfAdvertService getBeanSelfAdvertService() {
		return beanSelfAdvertService;
	}
	public void setBeanSelfAdvertService(
			IBeanSelfAdvertService beanSelfAdvertService) {
		this.beanSelfAdvertService = beanSelfAdvertService;
	}
	public String findTextList(){
		adverts=beanSelfAdvertService.findTextList();
		if(adverts.size()==0){appendObj(10,"1");}
		Criteria criteria = this.createCriteria(BeanSelfAdvert.class)
		.add(Restrictions.eq("adverttype", "1"));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "selfAdvertAction!findList.action", 10);
		return "findTextList";
	}
	public String findImgList(){
		adverts=beanSelfAdvertService.findImgList();
		if(adverts.size()==0){appendObj(10,"2");}
		Criteria criteria = this.createCriteria(BeanSelfAdvert.class)
		.add(Restrictions.eq("adverttype", "2"));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "selfAdvertAction!findImgList.action", 10);
		return "findImgList";
	}
	//更新评论状态
	public String updateStatus(){
		BeanSelfAdvert bean=beanSelfAdvertService.findById(this.getId(),BeanSelfAdvert.class);
		beanSelfAdvertService.setStatus(this.getId());
		if(bean.getAdverttype().equals("1")){
			return findTextList();
		}else{return findImgList();}
	}

	public String deleteSelfAdvert(){
		BeanSelfAdvert bean=beanSelfAdvertService.findById(this.getId(),BeanSelfAdvert.class);
		beanSelfAdvertService.delete(this.getId(), BeanSelfAdvert.class);
		if(bean.getAdverttype().equals("1")){
			return findTextList();
		}else{return findImgList();}
	}
	
	private  void appendObj(int num,String type) {
		// TODO Auto-generated method stub
		BeanSelfAdvert bean=null;
		for(int i=0;i<num;i++){
			bean=new BeanSelfAdvert();
			bean.setEnterpricename("中华科技有限公司");
			bean.setEmail("zk@345.com");
			bean.setLinkman("张封魔");
			bean.setTitle("好东西"+i);
			bean.setContent("好东西的内容展示"+i);
			bean.setFax("123-098765");
			BeanUser u=beanSelfAdvertService.findById(5, BeanUser.class);
			bean.setUser(u);
			bean.setStatus("未审核");
			if(type.equals("1")){
				bean.setAdverttype("1");
			}
			if(type.equals("2")){bean.setAdverttype("2");bean.setImgurl("images/"+(i+1)+".png");}
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			beanSelfAdvertService.save(bean);
		}
	}
}
